package com.jprocms.module.cms.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Cookie 辅助类
 */
public class CookieUtils {
    /**
     * 获得cookie
     *
     * @param request HttpServletRequest
     * @param name    cookie name
     * @return if exist return cookie, else return null.
     */
    public static Cookie getCookie(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie c : cookies) {
                if (c.getName().equals(name)) {
                    return c;
                }
            }
        }
        return null;
    }

    /**
     * 是否存在cookie
     *
     * @param request HttpServletRequest
     * @param name    cookie name
     * @return if exist return cookie, else return null.
     */
    public static boolean existCookie(HttpServletRequest request, String name) {
        Cookie cookie = getCookie(request, name);
        if (cookie != null) {
            return true;
        }
        return false;
    }

    /**
     * 根据部署路径，将cookie保存在根目录。
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @param name     名称
     * @param value    值
     * @param expiry   过期时间 秒为单位
     * @param domain   域名
     * @param httpOnly
     * @return
     */
    public static Cookie addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value,
                                   Integer expiry, String domain, String path, Boolean httpOnly, boolean isSecurity) {
        Cookie cookie = new Cookie(name, value);
        if (expiry != null) {
            cookie.setMaxAge(expiry);
        }
        if (StringUtils.isNotBlank(domain)) {
            cookie.setDomain(domain);
        }
        String ctx = request.getContextPath();
        cookie.setPath(StringUtils.isBlank(ctx) ? "/" : ctx);
        if (StringUtils.isNotBlank(path)) {
            if (cookie.getPath().endsWith("/")) {
                cookie.setPath(cookie.getPath() + path);
            } else {
                cookie.setPath(cookie.getPath() + "/" + path);
            }
        }
        cookie.setHttpOnly(httpOnly);
        cookie.setSecure(isSecurity);
        /**不可加上这个属性，内容、栏目预览功能需要借助cookie实现跨平台切换查看读取cookie，加上后读取不到cookie*/
        //cookie.setSecure(true);
        if ("https".equals(request.getScheme())) {
            cookie.setSecure(true);
        }
        response.addCookie(cookie);
        return cookie;
    }

    /**
     * 将cookie保存在指定目录。
     *
     * @param response HttpServletResponse
     * @param name     名称
     * @param value    值
     * @param expiry   过期时间 秒为单位
     * @param domain   域名
     * @Title: addCookie
     * @return: Cookie
     */
    public static Cookie addCookie(HttpServletResponse response, String name, String value,
                                   Integer expiry, String domain, boolean isSecurity) {
        Cookie cookie = new Cookie(name, value);
        if (expiry != null) {
            cookie.setMaxAge(expiry);
        }
        if (StringUtils.isNotBlank(domain)) {
            cookie.setDomain(domain);
        }
        cookie.setHttpOnly(true);
        cookie.setSecure(isSecurity);
        /**不可加上这个属性，内容、栏目预览功能需要借助cookie实现跨平台切换查看读取cookie，加上后读取不到cookie*/
        //cookie.setSecure(true);
        response.addCookie(cookie);
        return cookie;
    }

    /**
     * 取消cookie
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @param name     名称
     * @param domain   域名
     */
    public static void cancleCookie(HttpServletRequest request, HttpServletResponse response, String name,
                                    String domain) {
        Cookie cookie = new Cookie(name, "");
        cookie.setMaxAge(0);
        String ctx = request.getContextPath();
        cookie.setPath(StringUtils.isBlank(ctx) ? "/" : ctx);
        if (StringUtils.isNotBlank(domain)) {
            cookie.setDomain(domain);
        }
        response.addCookie(cookie);
    }
}
